﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Server.CacheData;
using CPPEI.Coolzon.Service;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    class ReqLeaveDomainHandler : BaseHandler<ReqLeaveDomainData, RespLeaveDomainData>
    {
        public ReqLeaveDomainHandler()
            : base(RESPONSE_SCENARIO.CONNENT, RESPONSE_COMMAND.CONNECT.LEAVEDOMAIN)
        {
        }

        protected override RespLeaveDomainData OnHandle(Data.MainServiceData.Request request, ReqLeaveDomainData requestData)
        {
            ControlDomain domain = CacheData.ServerData.Instance.GetDomain(requestData.DomainId);
            Client client = CacheData.ServerData.Instance.GetClient(request.Header.UserIdentity);
            if (domain != null)
            {
                if (client != null)
                {
                    domain.RemoveClient(client);
                    client.State = ClientState.InHall;
                }
                CacheData.ServerData.Instance.DeleteDomain(domain);
            }
            if (client != null)
            {
                CacheData.ServerData.Instance.RemoveClient(client);
            }
            return new RespLeaveDomainData();
        }
    }
}
